﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace DbContextString
{
    public class CollectService
    {

        private readonly CollectContext _collectContext;
        public CollectService(CollectContext collect)
        {
            _collectContext = collect;
        }

        public async Task CreateDatabaseAsync()
        {
            bool created = await _collectContext.Database.EnsureCreatedAsync();
            string creationInfo = created ? "created" : "exists";
            Console.WriteLine($"Database{creationInfo}");
        }


        public async Task AddCollectAsync()
        {
            var a1 = new Collect
            {
                Describe = "1",
                CtDateTime = DateTime.Now
            };

            var a2 = new Collect
            {
                Describe = "2",
                CtDateTime = DateTime.Now
            };
            _collectContext.AddRange(a1, a2);
            int recode = await _collectContext.SaveChangesAsync();
            Console.WriteLine($"{recode} OK");
        }

        public async Task ReadCollectAsync()
        {
            List<Collect> collects = await _collectContext.collects.ToListAsync();
            foreach (var b in collects)
            {
                Console.WriteLine($"{b.Describe} {b.CtDateTime}");
            }
            Console.WriteLine();
        }


    }
}
